﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ActionSystem.Core;
using ActionSystem.Core.Utils;

namespace ActionSystem.Timer
{
    [PoolInit(typeof(GeneticPool<ActionSystemTimer>))]
    public class ActionSystemTimer : IActionSystemSync,IPoolItem
    {
        private int index = 0;
        private int frameLength = 0;
        private bool inited = false;
        private bool updateBeforeSM = true;
        public bool IsDeleted { get; set; }

        private event Action startHandler;
        private event Action<int,float> updateHandler;
        private event Action endHandler;
        public ActionSystemTimer() { }
        public void Init(int frameCount,Action start,Action<int,float> update,Action end,bool updateBeforeSM=true)
        {
            inited = true;
            this.updateBeforeSM = updateBeforeSM;
            if (frameLength<0)
            {
                frameLength = 0;
            }
            frameLength = frameCount;
            startHandler += start;
            updateHandler += update;
            endHandler += end;
        }
        public void Start()
        {
            if (!inited)
                return;
            if(updateBeforeSM)
                ActionSystemManager.Instance.AddBeforeSync(this);
            else
                ActionSystemManager.Instance.AddAfterSync(this);
        }
        public void Pause()
        {
            if (!inited)
                return;
            if (updateBeforeSM)
                ActionSystemManager.Instance.RemoveBeforeSync(this);
            else
                ActionSystemManager.Instance.RemoveAfterSync(this);


        }
        public void Stop()
        {
            if (!inited)
                return;
            index = frameLength;
        }
        public void Update(float fixedUpdate)
        {
            if(inited==false)
            {
                return;
            }
            if(0==index)
            {
                startHandler?.Invoke();
            }
            if(index==frameLength)
            {
                endHandler?.Invoke();
                if (updateBeforeSM)
                    ActionSystemManager.Instance.RemoveBeforeSync(this);
                else
                    ActionSystemManager.Instance.RemoveAfterSync(this);

                PoolItemDelete();
            }
            if(index<frameLength)
            {
                updateHandler?.Invoke(index,fixedUpdate);                
            }
            index += 1;
        }
        /// <summary>
        /// 用户不可调用
        /// </summary>
        public void PoolItemReset()
        {
            inited = false;
            index=0;
            frameLength = 0;
            startHandler = null;
            updateHandler = null;
            endHandler = null;
        }
        /// <summary>
        /// 用户不可调用
        /// </summary>
        public void PoolItemDelete()
        {
            GeneticPool<ActionSystemTimer>.Delete(this);
        }
    }
}
